package com.dy.seleniumlearn;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolver.iterator;

public class WebElementOp4 {
    WebDriver driver;
    WebDriver.Navigation navigation;

    @BeforeMethod
    public void SetUp() {
        System.setProperty("webdriver.gecko.driver", "C:\\Program Files\\Mozilla Firefox\\geckodriver.exe");
        driver = new FirefoxDriver();
        navigation = driver.navigate();
    }

    @Test(enabled = false)
    public void test() throws InterruptedException {
        driver.get("http://39.101.198.110/testhtml/exam/testSelenium.html");
        driver.findElement(By.cssSelector("[value='Alert']")).click();
        System.out.println(driver.switchTo().alert().getText());
        Thread.sleep(3000);
        // driver.switchTo().alert().accept();
        driver.switchTo().alert().dismiss();
    }

    @Test
    public void test2() throws InterruptedException {
        driver.get("http://39.101.198.110/testhtml/exam/testSelenium.html");
        driver.findElement(By.cssSelector("[value='prompt']")).click();
        driver.switchTo().alert().sendKeys("参数");
        System.out.println(driver.switchTo().alert().getText());
        Thread.sleep(3000);
        driver.switchTo().alert().dismiss();
    }

    @Test
    public void test3() {
        driver.get("D:\\exam\\index.html");
        driver.findElement(By.linkText("继续提供房源信息")).click();
        //多窗口抓取
        Set<String> hands = driver.getWindowHandles();
        Iterator<String> it = hands.iterator();
        while (it.hasNext()) {
            String next = it.next();
            System.out.println(next);
        }
        String[] strings = new String[hands.size()];
        hands.toArray(strings);
        driver.switchTo().window(strings[1]);
        driver.findElement(By.name("username")).clear();
        driver.findElement(By.name("username")).sendKeys("测试");
    }

    @Test
    public void test4() {
        driver.get("https://tieba.baidu.com/index.html");
        //获取cookie
        Set<Cookie> cookies = driver.manage().getCookies();
        Cookie[] cookies1 = new Cookie[cookies.size()];
        cookies.toArray(cookies1);
        for (int i = 0; i < cookies1.length; i++) {
            System.out.println("第i个cookie" + cookies1[i]);
        }

    }

    //window窗口大小位置设置
    @Test
    public void test5() {
        driver.get("https://tieba.baidu.com/index.html");
        //最大化
        driver.manage().window().maximize();
        driver.manage().window().setSize(new Dimension(400, 500));
        driver.manage().window().setPosition(new Point(20, 50));
        System.out.println(driver.manage().window().getPosition().getX());
        System.out.println(driver.manage().window().getPosition().getY());
        System.out.println(driver.manage().window().getSize());
    }

    //设置时间内做完通过，没做完报异常
    @Test
    public void test6() {
        driver.get("https://tieba.baidu.com/index.html");
        WebDriverWait webDriverWait = new WebDriverWait(driver, 5);
        webDriverWait.until(new ExpectedCondition<WebElement>() {
            @Override
            public WebElement apply(WebDriver webDriver) {
                return driver.findElement(By.id("wd1"));
            }
        }).sendKeys("测试");
        driver.findElement(By.linkText("进入贴吧")).click();
    }

    //设置时间内做完通过，没做完报异常
    @Test
    public void test7() {
        WebDriver.Timeouts timeouts = driver.manage().timeouts();
        timeouts.pageLoadTimeout(5, TimeUnit.SECONDS);
        driver.get("https://tieba.baidu.com/index.html");
        timeouts.implicitlyWait(5, TimeUnit.SECONDS);
        driver.findElement(By.id("wd1"));
        //timeouts.pageLoadTimeout(5, TimeUnit.SECONDS);
        // timeouts.setScriptTimeout(10, TimeUnit.SECONDS);
        //  driver.findElement(By.id("wd1")).sendKeys("测试");
        //timeouts.pageLoadTimeout(5, TimeUnit.SECONDS);
    }

    //框架里面的元素抓取
    @Test
    public void test8() {
        driver.get("D:\\workspaceHtml\\iframeDemo.html");
        driver.switchTo().frame("w1").findElement(By.linkText("test1")).click();
        //driver.findElement(By.linkText("t1127")).click();
        driver.switchTo().parentFrame().switchTo().frame("w2");
        driver.findElement(By.linkText("t1127")).click();
    }

    //设置时间内做完通过，没做完报异常
    @AfterMethod
    public void afterMethod() {
        WebDriver.Timeouts timeouts = driver.manage().timeouts();
        timeouts.setScriptTimeout(10, TimeUnit.SECONDS);
        //  driver.close();
    }
}
